package lanou.a36ke_01.welcome;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import lanou.a36ke_01.R;
import lanou.a36ke_01.activity.MainActivity;
import lanou.a36ke_01.tools.BaseActivity;


public class WelcomeActivity  extends BaseActivity implements View.OnClickListener {

    private LinearLayout layout;
    private TextView textView;
    private CountDownTimer timer;

    @Override
    protected int setLayout() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initView() {
        layout = bindView(R.id.activity_welcome);
        textView = bindView(R.id.Welcome_text);
    }

    @Override
    protected void initDate() {
        textView.setOnClickListener(this);
        WelcomeAsyncTask asyncTask = new WelcomeAsyncTask();
        asyncTask.execute("https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1472712109139&di=e18aab907bc0cab56deb89d9b420cd61&imgtype=jpg&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01be78558b6456000000b18f417777.jpg");
        timer = new CountDownTimer(3000,5000) {
            @Override
            public void onTick(long millisUntilFinished) {
                String time = millisUntilFinished / 1000 + "s" ;
                textView.setText(time);
            }

            @Override
            public void onFinish() {

                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }.start();
    }

    @Override
    public void onClick(View v) {
        timer.cancel();
        Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
        startActivity(intent);
        finish();

    }

    private class WelcomeAsyncTask extends AsyncTask<String,Integer,Bitmap>{
        private Bitmap bitmap;
        @Override
        protected Bitmap doInBackground(String... params) {
            String s = params[0];
            try {
                URL url = new URL(s);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    InputStream is = connection.getInputStream();
                    bitmap = BitmapFactory.decodeStream(is);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            layout.setBackgroundDrawable(new BitmapDrawable(bitmap));
        }
    }
}
